package com.doubao.common.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.stereotype.Component;

/**
 * Redis连接工厂初始化检查器
 * 用于在应用启动时检查和记录Redis连接配置的实际情况
 */
@Component
@Slf4j
public class RedisConnectionFactoryInitializer implements InitializingBean {

    private final RedisConnectionFactory connectionFactory;

    public RedisConnectionFactoryInitializer(RedisConnectionFactory connectionFactory) {
        this.connectionFactory = connectionFactory;
    }

    @Override
    public void afterPropertiesSet() {
        if (connectionFactory instanceof LettuceConnectionFactory) {
            LettuceConnectionFactory lcf = (LettuceConnectionFactory) connectionFactory;
            log.info("【Redis最终配置】 host={}, port={}, db={}",
                    lcf.getHostName(), lcf.getPort(), lcf.getDatabase());

            if ("localhost".equals(lcf.getHostName()) || "127.0.0.1".equals(lcf.getHostName())) {
                log.warn("警告：Redis仍然配置为本地地址! 这可能不是您期望的配置。");
            }
        } else {
            log.info("当前RedisConnectionFactory类型: {}",
                    connectionFactory != null ? connectionFactory.getClass().getName() : "null");
        }
    }
}